
package com.hxrk.cecardvr;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.hxrk.aidlservice.DvrServiceManager;

import org.opencv.android.OpenCVLoader;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MyApplication extends MultiDexApplication {
    public static final String TAG = "CECarDvr";

    public boolean OneceStar = true; // 第一次运行关闭ACtivity

    public static SharedPreferences sPreferences;
    public static SharedPreferences.Editor editor;

    public static int g_Previewstatus; // 0预览关闭 1预览开启

    public static DvrServiceManager mDvrManager;

    //public static String FilePathName = "/DidiMain/Pictures";

    //拍照队列
    public static Map<Integer, Bundle> photoBundles = new HashMap<>();

    //905报警标志
    public static int BJBZ905 = 0;

    public static Context mContext;


    public static String[] mainPackages = {"com.hxrk.carmain", "com.cekj.didimain"};
    public static String mainPackage = "";

    //定义浮动窗口布局
    public LinearLayout mFloatLayout;
    public WindowManager.LayoutParams wmParams;
    //创建浮动窗口设置布局参数的对象
    public WindowManager mWindowManager;
    public static SurfaceView mPrevewviewCam;


    @Override
    protected void attachBaseContext(Context base) {
        mContext = this;
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    // TODO
    @Override
    public void onCreate() {
        super.onCreate();

        try {
            //初始化人脸识别
            if (!OpenCVLoader.initDebug()) {
                Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            } else {
                Log.d(TAG, "OpenCV library found inside package. Using it!");
//            mOpenCvCameraView.enableView();
            }

            wmParams = new WindowManager.LayoutParams();
            //获取的是WindowManagerImpl.CompatModeWrapper
            mWindowManager = (WindowManager) this.getSystemService(this.WINDOW_SERVICE);

            //设置window type
            wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
            //设置图片格式，效果为背景透明
            wmParams.format = PixelFormat.RGBA_8888;
            //设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
            wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            //调整悬浮窗显示的停靠位置为左侧置顶
            wmParams.gravity = Gravity.LEFT | Gravity.TOP;
            // 以屏幕左上角为原点，设置x、y初始值，相对于gravity
            wmParams.x = 0;
            wmParams.y = 0;

            //设置悬浮窗口长宽数据
            wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
            wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

//            // 设置悬浮窗口长宽数据
//            wmParams.width = 200;
//            wmParams.height = 80;

            LayoutInflater inflater = LayoutInflater.from(this);
            //获取浮动窗口视图所在布局
            mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);
            //添加mFloatLayout
            mWindowManager.addView(mFloatLayout, wmParams);
            //浮动窗口按钮
            mPrevewviewCam = (SurfaceView) mFloatLayout.findViewById(R.id.SurfaceViewPlay);


            PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0);

            mDvrManager = DvrServiceManager.getInstance();
            new Thread() {
                public void run() {
                    while (true) {
                        if (!mDvrManager.bServiceConn)
                            mDvrManager.bindMpospService(getApplicationContext());
                        try {
                            sleep(15000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();

            sPreferences = getApplicationContext().getSharedPreferences("MediaID_Info", 0);
            editor = sPreferences.edit();
            CamService.isACC = true;//sPreferences.getBoolean("ISACC", false);
            Utils.putTextCPHM = sPreferences.getString("TextCPHM", "test");
            CamService.isRebootERR = sPreferences.getBoolean("IS_REBOOT_ERR", false);
            for (int i = 0; i < Utils.fileNumber.length; i++) {
                Utils.fileNumber[i] = sPreferences.getInt("FileNumber" + i, 0);
            }
            CamService.PTType = sPreferences.getInt("PTType", 1);
            CamService.width1_v = sPreferences.getInt("width1_v", 640);
            CamService.height1_v = sPreferences.getInt("height1_v", 480);
            CamService.width2_v = sPreferences.getInt("width2_v", 640);
            CamService.height2_v = sPreferences.getInt("height2_v", 480);

            Intent intent = new Intent(getApplicationContext(), CamService.class);
            startService(intent);

//            onStartService();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //启动人脸识别服务
    private void onStartService() {
        LogUtils.d("启动人脸识别服务");
        Intent intent = new Intent(mContext, FaceService.class);
        this.startService(intent);
    }

    private static int lastmediaid = 0;

    /**
     * 根据时间生成ID
     */
    public static int generateID(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int year = c.get(Calendar.YEAR) - CamService.PTType == 1 ? 2000 : 2010;
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);

        return ((year << 26) & 0xFC000000) | ((month << 22) & 0x3C00000) | ((day << 17) & 0x3E0000) | ((hour << 12) & 0x1F000) | ((minute << 6) & 0xFC0) | (second & 0x3F);
    }

    public static int GetMediaID() {
        int id = generateID(new Date());
        if (id == lastmediaid) {
            try {
                Thread.sleep(1200);
            } catch (Exception e) {
                e.printStackTrace();
            }
            id = generateID(new Date());
        }
        lastmediaid = id;
        return id;
    }

    public static boolean getAlarm() {
        return (((MyApplication.BJBZ905 >> 0) & 0x01) == 1 || ((MyApplication.BJBZ905 >> 1) & 0x01) == 1) ? true : false;
    }
}
